package cn.cvs.web.controller;

import cn.cvs.pojo.Auction;
import cn.cvs.utils.SimpleBeanConvertor;
import cn.cvs.utils.UnsupportedTypeException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.UUID;

//web 3.0 的注解
//@WebServlet(name ="AuctionServlet",urlPatterns = "/auction/AuctionServlet")
public class AuctionServlet  {

    public String getAuctionList(HttpServletRequest request, HttpServletResponse response){
        System.out.println("*************getAuctionList************");
        return "/index.jsp";
    }

    public String updateAuction(HttpServletRequest request, HttpServletResponse response){
        System.out.println("*********updateAuction***********");

        //获取表单基础数据
        Auction auction =new Auction();
       /* auction.setAuctionName(request.getParameter("auctionName"));
        auction.setAuctionStartPrice(request.getParameter("auctionStartPrice"));
        auction.setAuctionUpset(request.getParameter("auctionUpset")); */
        try {
            //获取文件信息
            Part part = request.getPart("auctionPic");// <input name="auctionPic" type="file" class="offset10 lf" />
            auction = SimpleBeanConvertor.convert(request,Auction.class);
            System.out.println(auction);
            System.out.println("文件名: "+part.getSubmittedFileName());

            String server_url= "D:\\temp\\files\\";//服务器地址


            //文件上传
            //取到jsp页面中的file的文件
            //文件名
            String fileName = part.getSubmittedFileName();

            fileName=getFileName(fileName);

            fileName =server_url.concat(fileName);

            part.write(fileName);

            System.out.println("处理后的文件名: "+fileName);

            auction.setAuctionPic(fileName);

            //新增或者修改到数据库中

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (UnsupportedTypeException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException exception) {
            exception.printStackTrace();
        }

        return "redirect:/do/auction/list";
    }


    private static String getFileName(String realFileName) {
        String uuid = UUID.randomUUID().toString();
        uuid =uuid.replace("-","");
        String ext =realFileName;
        ext =ext.substring(ext.lastIndexOf("."));
        return uuid+ext;
    }


   /* @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("***********************AuctionServlet");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }*/
}
